package com.nervous.jiaoli.user.login;

import com.nervous.jiaoli.common.Result;
import com.nervous.jiaoli.common.TokenUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @RestController = @Controller + @ResponseBody
 * @Controller: 将当前修饰的类注入SpringBoot IOC 容器
 * @ResponseBody: 将修饰的类的返回值转化为Json格式（String类型仍保留String）
 */
@RestController
@RequestMapping("/user/login")
public class LoginAPI {
    @Resource
    private LoginService loginService;

    @PostMapping("")
    public Result login(@RequestBody LoginDto dto) {
        boolean ok = loginService.checkLogin(dto);
        if (ok) {
            String token = TokenUtils.loginSign(dto.getU_id(), dto.getU_pwd());
            return Result.success((Object)token); // 向客户端发送成功信号，并发送令牌
        }
        return Result.fail(Result.ERR_CODE_BUSINESS, "账号或密码错误！");
    }
}
